<template>
    <div class="progress" ref="progress">
        <div class="drag-start"></div>
        <div class="drag" ref="drag"
        :style="{position: 'absolute',left: 'calc('+ progressRate +'% - 7px)'}"
        @touchmove.prevent="touchMove">
            <span></span>
        </div>
        <p class="line-b"></p>
        <p class="current" :style="{width: progressRate + '%'}"></p>
    </div>
</template>
<script>
export default {
  props: {
    currentP: {
        type: Number,
        default: 0
    },
    progressSumUp: {
        type: Number,
        default: 0
    },
    minProgress: {
        type: Number,
        default: 0
    }
  },
  data () {
    return {
      progressRate: 100
    }
  },
  methods: {
    touchMove (e) {    
        const pageX = e.touches[0].pageX // 距离屏幕左边的距离
        const progressLeft = this.$refs.progress.getBoundingClientRect().left // 整个滚动条距离页面左边的距离
        const progressRight = this.$refs.progress.getBoundingClientRect().right // 整个滚动条最右边距离页面左边的距离
        let progressRate = (pageX - progressLeft) / this.$refs.progress.clientWidth * 100 // 拖动的点距离滚动条左边的距离百分比
        
        if (progressRate <= 0) { // 超出左边就为0%
            progressRate = 0
        } else if (progressRate >= 100) { // 超出右边就为100%
            progressRate = 100
        }
        let cp = ((progressRate / 10 * this.progressSumUp) / 10) | 0
        if(cp < this.minProgress){
            return
        }
        this.progressRate = progressRate
        this.$emit('change', cp)
    }
  }
}
</script>

<style lang="scss" scoped>
.progress{
    width: 100%;
    height: 14px;
    position: relative;
}
.line-b{
    width: 100%;
    height: 2px;
    background-color:#DFE8FF;
    position: absolute;
    left: 0;
    border-radius: 3px;
    top:0;
    bottom: 0;
    margin: auto;
    z-index: 1;
}
.drag-start{
    width: 6px;
    height: 6px;
    position: absolute;
    background-color: $color-primary;
    border-radius: 50%;
    left: -2px;
    top: 0px;
    bottom: 0px;
    margin: auto;
}
.drag{
    width: 12px;
    height: 12px;
    top: 0px;
    bottom: 0px;
    margin: auto;
    background-color: $color-primary;
    border-radius: 50%;
    z-index: 3;
    // position: relative;
    // span{
    //     width: 3px;height: 3px;
    //     background-color: $color-primary;
    //     position: absolute;
    //     left: 0;
    //     right: 0;
    //     top: 0;
    //     bottom: 0;
    //     margin: auto;
    //     display: block;
    //     border-radius: 50%;
    // }
}
.current{
    height: 3px;
    background-color: $color-primary;
    position: absolute;
    left: 0;
    border-radius: 0.01rem;
    top:0;
    bottom: 0;
    margin: auto;
    z-index: 2;
}
</style>